/*****************************************************************************
*    Ali Corp. All Rights Reserved. 2002 Copyright (C)
*
*    File:    pan_sm_1668.S
*
*    Description:    NEC 16312 panel driver code in standby mode.
*    History:
*           Date            Athor        Version          Reason
*	    ============	=============	=========	=================
*	1.	10.26.2006       Wen Liu        Ver 0.1    Implemnt in M3330C Bootloader.
*	2.	06.16.2007	  Goliath Peng  Ver 0.3	  Porting NEC 16312 code for PT 1668	
*****************************************************************************/

#include <hal/mips.h>
#include <hld/pan/pan.h>
#include <sys_config.h>

#define GPIO_OUTPUT_5V 0

//SG1:0,SG2:1,SG3:2 ... SG16:15
#define START_SG_TO_LED 2
#define RED_LED_CONTROL_SG 12
#define GRE_LED_CONTROL_SG 13

#define HAL_GPIO_DI_REG	0xb8000050
#define HAL_GPIO_DO_REG	0xb8000054
#define HAL_GPIO_DIR_REG		0xb8000058

#define HAL_GPIO1_DIR_REG	0xb80000d8
#define HAL_GPIO1_DI_REG		0xb80000d0
#define HAL_GPIO1_DO_REG	0xb80000d4
#define HAL_GPIO2_DIR_REG	0xb80000ec
#define HAL_GPIO2_DI_REG		0xb80000e4
#define HAL_GPIO2_DO_REG	0xb80000e8
#define HAL_GPIO3_DIR_REG	0xb80000f8
#define HAL_GPIO3_DI_REG		0xb80000f0
#define HAL_GPIO3_DO_REG	0xb80000f4

/*************************************************************************/
/*************************************************************************/
#define CMD1_PREFIX			0x40

#define SM1668_NORMAL_MODE	(0<<3)
#define SM1668_TEST_MODE		(1<<3)

#define INCRE_ADDR		(0<<2)
#define FIXED_ADDR		(1<<2)

#define WRITE_DATA		(0)
#define READ_KEY		(2)

#define CMD1_READ		(CMD1_PREFIX|SM1668_NORMAL_MODE|INCRE_ADDR|READ_KEY)
//#define CMD1_READ		(CMD1_PREFIX|SM1668_NORMAL_MODE|FIXED_ADDR|READ_KEY)

#define CMD1_WRITE_INCRE		(CMD1_PREFIX|SM1668_NORMAL_MODE|INCRE_ADDR|WRITE_DATA)
#define CMD1_WRITE_FIXED		(CMD1_PREFIX|SM1668_NORMAL_MODE|FIXED_ADDR|WRITE_DATA)

/************************************************************************/
#define CMD2_PREFIX		0xc0
#define CMD2_ADDR(addr)	(CMD2_PREFIX|(addr&0xf))

/************************************************************************/
#define CMD3_PREFIX		0x80

#define DISPLAY_OFF		(0<<3)
#define DISPLAY_ON		(1<<3)

#define DIM_1_16		0
#define DIM_2_16		1
#define DIM_4_16		2
#define DIM_10_16		3
#define DIM_11_16		4
#define DIM_12_16		5
#define DIM_13_16		6
#define DIM_14_16		7

#define LED_OFF			(CMD3_PREFIX|DISPLAY_OFF|DIM_2_16)
#define LED_ON			(CMD3_PREFIX|DISPLAY_ON|DIM_2_16)

####################Macro#####################
# MACRO: get_stb_gpio(reg dr)
#  reg dr: load stb_gpio to dr.
#
		.macro	get_stb_gpio dr
		la		t2, stb_gpio
		lw		\dr, 0(t2)
		nop
		.endm

# MACRO: get_clk_gpio(reg dr)
#  reg dr: load clk_gpio to dr.
#
		.macro	get_clk_gpio dr
		la		t2, clk_gpio
		lw		\dr, 0(t2)
		nop
		.endm
		
# MACRO: get_data_gpio(reg dr)
#  reg dr: load data_gpio to dr.
#
		.macro	get_data_gpio dr
		la		t2, data_gpio
		lw		\dr, 0(t2)
		nop
		.endm

####################SET GPIO bit to required value #####################
#		used a0,a1,a3,t0,t1,t2,t6,s1,ra
#		void hal_gpio_setbit_SM1668(int gpio_polar, struct pan_hw_info *pan_hw_info)  
#		a0:  gpio_polar; a1: pan_hw_info
######################################################################
		.globl	hal_gpio_setbit_sm1668
		.ent	hal_gpio_setbit_sm1668
hal_gpio_setbit_sm1668:
		.set	noreorder
		move	s1,ra
		
#Get data in gpio num										
		move		t2, a1
# .word	0x7000003f		
		andi		a3, a3, 0x00
#		ori		a3, a3, 0x4b  
		get_data_gpio a3

#		nop
#		nop
#		sdbbp
#		nop
#		nop
		
# Read GPIO DO reg									 
		andi		t2, a3, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a3, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a3, 0x1f
		li		t2,1
		sllv		t2,t2,a3	#a3 = DIN gpio number, t2 = DIN gpio mask, t0 = DIN GPIO_DO_REG

#En DIN GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
#delay									
		jal		delay_us_1668
		li		t6,1
		
#Pull high or pull low DIN GPIO pin									
		lw		t1, 0(t0)					
#if(GPIO_OUTPUT_5V == 1)
		beqz		a0, 1f
		nop
		j		2f			
		or		t1,t1,t2	#Output bit 1
1:		
#endif
		not		t2
		and		t1, t1, t2
2:		sw		t1,0(t0)

#if(GPIO_OUTPUT_5V == 0)
		beqz	a0, 3f
		nop
		lw		t1, 4(t0)
		li		t2, 1
		sllv		t2, t2,a3	#a3 = DIN gpio number, t2 = DIN gpio mask, t0 = DIN GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis DIN output mode for pull high DIN pin		
3:
#endif	

#Get CLK gpio num										
		move		t2, a1
		andi		a3, a3, 0x00
#		ori		a3, a3, 0x3e  // 
		get_clk_gpio a3

# Read GPIO DO reg									 
		andi		t2, a3, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a3, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a3, 0x1f		
		li		t2, 1
		sllv		t2,t2,a3	#a3 = CLK gpio number, t2 = CLK gpio mask, t0 = CLK GPIO_DO_REG

#En CLK GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
#delay									
		jal		delay_us_1668
		li		t6,1

#Pull low CLK gpio pin		
		lw		t1, 0(t0)	
		not		t2
		and		t1, t1, t2
		sw		t1, 0(t0)	

#delay		
		jal		delay_us_1668
		li		t6,1

#Pull high CLK gpio pin		
		lw		t1, 0(t0)	
		not		t2
		or		t1, t1, t2
		sw		t1, 0(t0)
#if(GPIO_OUTPUT_5V == 0)
#Dis CLK GPIO output
		lw		t1, 4(t0)
		li		t2, 1
		sllv		t2, t2,a3	#a3 = CLK gpio number, t2 = CLK gpio mask, t0 = CLK GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis CLK output mode for pull high CLK pin		
#endif
		
#return		
		move	ra,s1
		jr		ra
		nop
		.set	reorder		
		.end	hal_gpio_setbit_sm1668

###################GET GPIO bit  #####################
#		used a0,a3,t0,t1,t2,t6,v0,s1,ra
#		UINT32 hal_gpio_getbit_sm1668(struct pan_hw_info *pan_hw_info)
#		v0: return value;	a0: pan_hw_info
###################################################
		.globl	hal_gpio_getbit_sm1668
		.ent	hal_gpio_getbit_sm1668
hal_gpio_getbit_sm1668:
		.set	noreorder
		move	s1,ra
#Get DOUT gpio num										
		move		t2, a0
		andi		a3, a3, 0x00
#		ori		a3, a3, 0x4b  // 
		get_data_gpio a3

# Read GPIO DO reg									 
		andi		t2, a3, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a3, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a3, 0x1f		
		li		t2, 1
		sllv		t2,t2,a3	#a3 = DOUT gpio number, t2 = DOUT gpio mask, t0 = DOUT GPIO_DO_REG
		
#Dis DOUT gpio output mode		
		lw		t1, 4(t0)
		not		t2
		and		t1,t1,t2
		sw		t1,4(t0)

#delay		
		jal		delay_us_1668
		li		t6,1

#Get CLK gpio num										
		srl		a3, 2
		andi		a3, a3, 0x00
#		ori		a3, a3, 0x3e  // 
		get_clk_gpio a3
		

# Read GPIO DO reg									 
		andi		t2, a3, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a3, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a3, 0x1f		
		li		t2, 1
		sllv		t2,t2,a3	#a3 = CLK gpio number, t2 = CLK gpio mask, t0 = CLK GPIO_DO_REG

#En CLK GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)

#Pull low CLK gpio pin		
		lw		t1, 0(t0)
		not		t2
		and		t1, t1, t2
		sw		t1, 0(t0)

#delay		
		jal		delay_us_1668
		li		t6,1
		
#Get DOUT gpio num										
		move		t2, a0
		andi		a3, a3, 0x00
	#	ori		a3, a3, 0x4b  // 
		get_data_gpio a3
	

# Read GPIO DI reg
		andi		t2, a3, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DI_REG
		j		2f
		nop
1:									 
		li		t0, HAL_GPIO_DI_REG
		andi		t2, a3, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DI_REG	
2:		
		andi		a3, 0x1f		
		li		t2, 1
		sllv		t2,t2,a3	#a3 = DOUT gpio number, t2 = DOUT gpio mask, t0 = DOUT GPIO_DI_REG

#Read DOUT input value		
		lw		v0, 0(t0)
		and		v0, v0, t2
		srlv		v0, v0, a3
		
#delay	
		jal		delay_us_1668
		li		t6,1

#Get CLK gpio num										
		move		t2, a0
		andi		a3, a3, 0x00
#		ori		a3, a3, 0x3e    // 
		get_clk_gpio a3
		

# Read GPIO DO reg									 
		andi		t2, a3, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a3, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a3, 0x1f		
		li		t2, 1
		sllv		t2,t2,a3	#a3 = CLK gpio number, t2 = CLK gpio mask, t0 = CLK GPIO_DO_REG

#Pull high CLK gpio pin		
		lw		t1, 0(t0)
		or		t1, t1, t2
		sw		t1, 0(t0)
#if(GPIO_OUTPUT_5V == 0)
#Dis CLK GPIO output
		lw		t1, 4(t0)
		li		t2, 1
		sllv		t2, t2,a3	#a3 = CLK gpio number, t2 = CLK gpio mask, t0 = CLK GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis CLK output mode for pull high CLK pin		
#endif
		
		move	ra,s1
		jr		ra
		nop
		.set	reorder		
		.end	hal_gpio_getbit_sm1668
		
####################Shift GPIO data out #####################
#		used a0,a1,v0,v1,t3,t5,s0,ra
#		void gpio_send_led_data_sm1668(UINT8 input,struct pan_hw_info *pan_hw_info)
#		a0 : input;	a1: pan_hw_info
########################a0 data[i]#######################
	.globl	gpio_send_led_data_sm1668
	.ent	gpio_send_led_data_sm1668
gpio_send_led_data_sm1668:
		.set	noreorder
		move		s0,ra
		or		t5,a0,a0
		li		t3,0
		li		v1,16
1:
		jal		hal_gpio_setbit_sm1668
		andi		a0,t5,1
		addiu		t3,1
		srl		t5,1
		
		slt		v0,t3,v1
		bnez		v0,1b
		move		ra,s0
		
		jr		ra
		nop
		.set	reorder
		.end	gpio_send_led_data_sm1668

####################Shift GPIO data out #####################
#		used a0,a1,v0,v1,t3,t5,s0,ra
#		void gpio_send_byte_sm1668(UINT8 input,struct pan_hw_info *pan_hw_info)
#		a0 : input;	a1: pan_hw_info
########################a0 data[i]#######################
	.globl	gpio_send_byte_sm1668
	.ent	gpio_send_byte_sm1668
gpio_send_byte_sm1668:
		.set	noreorder
		move		s0,ra
		or		t5,a0,a0
		li		t3,0
		li		v1,0x8
1:
		jal		hal_gpio_setbit_sm1668
		andi		a0,t5,1
		addiu		t3,1
		srl		t5,1
		
		slt		v0,t3,v1
		bnez		v0,1b
		move		ra,s0
		
		jr		ra
		nop
		.set	reorder
		.end	gpio_send_byte_sm1668

####################Shift GPIO data in #####################
#		used t0,v1,t5,t3,s0
#		UINT8 gpio_get_byte_sm1668(struct pan_hw_info *pan_hw_info)
#		v0 : return value;	a0: pan_hw_info
########################a0 data[i]#######################
	.globl	gpio_get_byte_sm1668
	.ent	gpio_get_byte_sm1668
gpio_get_byte_sm1668:
		.set	noreorder
		move		s0,ra
		li		t5,0
		li		t3,0
		li		v1,0x8
1:
		jal		hal_gpio_getbit_sm1668		
		nop
		
		andi		t0,v0,1
		sllv		t0,t0,t3
		or		t5,t5,t0
		addiu		t3,1
		slt		v0,t3,v1
		bnez		v0,1b
		nop
		move		v0, t5
		move		ra,s0
		jr		ra
		nop
		.set		reorder
		.end		gpio_get_byte_sm1668

####################show_led_sm1668 #####################
#		used a0,a1,a2,t0,t1,t2,t3,t4,t6,t7,t9,ra,s6
#		void show_led_sm1668(struct pan_hw_info *pan_hw_info,UINT8* led_array) 
#		a0 : pan_hw_info; a1: led_array[]; t9,a3:0 show green led,1:show red led
###############################################
		.globl	show_led_sm1668
		.ent	show_led_sm1668
show_led_sm1668:
# .word	0x7000003f
		.set	noreorder
		move 		t7, s6
		move	a2, a0
		move	t9,a3
		move	t4, a1	
		li	t0, 0x10
		andi	t1, t7, 0xf
		bgeu	t1, t0, show_led_exit
		nop
		andi t1, t7, 0xf0
		srl t1, t1, 4
		bgeu	t1, t0, show_led_exit
		nop
		andi t1, t7, 0xf00
		srl t1, t1, 8
		bgeu	t1, t0, show_led_exit
		nop
		andi t1, t7, 0xf000
		srl t1, t1, 12
		bgeu	t1, t0, show_led_exit
		nop
		move	t7, ra
#if 1		
		la      t1, 3f					# Running in cache
   		sll		t1, 4
   		srl		t1, 4
   		lui		t0, 0x8000
   		or		t1, t0
   		jr      t1
   		nop
#endif   		
3:   				
#Get STB gpio num										
		move		t2, a2
		andi		a1, a1, 0x00
#		ori		a1, a1, 0x45   // 
		get_stb_gpio a1
		

# Read GPIO DO reg									 
		andi		t2, a1, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a1, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a1, 0x1f		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG

#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)

#Pull high STB gpio pin
		lw		t1, 0(t0)
		or		t1,t1,t2
		sw		t1,0(t0)
#if(GPIO_OUTPUT_5V == 0)
#Dis STB GPIO output
		lw		t1, 4(t0)
		li		t2, 1
		sllv	t2, t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis STB output mode for pull high STB pin		
#endif
		
#Pull low STB gpio pin		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG
#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
		lw		t1, 0(t0)
		not		t2
		and		t1,t1,t2
		sw		t1,0(t0)

#delay		
		jal		delay_us_1668
		li		t6,5

#turn on led		
		li		a0, LED_ON
		move		a1, a2
		jal		gpio_send_byte_sm1668
		nop		
		nop 
		nop 
#		sdbbp
		nop 
		nop 		
#########	first index-1##################
#########	dot light ##################		
#delay		
		jal		delay_us_1668
		li		t6,5
		
#Get STB gpio num										
		move		t2, a2
		andi		a1, a1, 0x00
#		ori		a1, a1, 0x45  // 
		get_stb_gpio a1

		

# Read GPIO DO reg									 
		andi		t2, a1, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a1, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a1, 0x1f		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG

#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
#Pull high STB gpio pin
		lw		t1, 0(t0)
		or		t1,t1,t2
		sw		t1,0(t0)
#if(GPIO_OUTPUT_5V == 0)
#Dis STB GPIO output
		lw		t1, 4(t0)
		li		t2, 1
		sllv		t2, t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis STB output mode for pull high STB pin		
#endif
		
#delay		
		jal		delay_us_1668
		li		t6,5

#Pull low STB gpio pin		
		li		t2, 1
		sllv	t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG
#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
		lw		t1, 0(t0)
		not		t2
		and		t1,t1,t2
		sw		t1,0(t0)

#delay		
		jal		delay_us_1668
		li		t6,5
		
#write inc addr		
		li		a0, CMD1_WRITE_INCRE
		move	a1, a2
		jal		gpio_send_byte_sm1668	
#delay		
		jal		delay_us_1668
		li		t6,5
		
#Get STB gpio num										
		move		t2, a2
		andi		a1, a1, 0x00
#		ori		a1, a1, 0x45  // 
		get_stb_gpio a1


# Read GPIO DO reg									 
		andi		t2, a1, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a1, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:				
		andi		a1, 0x1f		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG

#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
#Pull high STB gpio pin
		lw		t1, 0(t0)
		or		t1,t1,t2
		sw		t1,0(t0)
#if(GPIO_OUTPUT_5V == 0)
#Dis STB GPIO output
		lw		t1, 4(t0)
		li		t2, 1
		sllv		t2, t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis STB output mode for pull high STB pin		
#endif		
#delay		
		jal		delay_us_1668
		li		t6,5

#Pull low STB gpio pin		
		li		t2, 1
		sllv	t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG
#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
		lw		t1, 0(t0)
		not		t2
		and		t1,t1,t2
		sw		t1,0(t0)

#delay		
		jal		delay_us_1668
		li		t6,5
		nop		
		nop 
		nop 
#		sdbbp
		nop 
		nop 	
#write start addr		
		li	a0, (CMD2_PREFIX|8)
		move	a1, a2
		jal	gpio_send_byte_sm1668
		nop	
		nop		
		nop 
		nop 
#		sdbbp
		nop 
		nop 			
#write 0	
		nop 
		nop 
#		sdbbp
		nop 
		nop 	
		move	t3, s6
		srl		t3, 16
		andi	t3,0x80
		nop
		sll		a0,t3,START_SG_TO_LED
		move	a1, a2
		jal	gpio_send_led_data_sm1668
		nop
		nop 
		nop 
#		sdbbp
		nop 
		nop 	
		
#Start update led display mem
#####if 1		

#delay		
		jal		delay_us_1668
		li		t6,5
		
#Get STB gpio num										
		move		t2, a2
		andi		a1, a1, 0x00
#		ori		a1, a1, 0x45  // 
		get_stb_gpio a1


# Read GPIO DO reg									 
		andi		t2, a1, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a1, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a1, 0x1f		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG

#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
#Pull high STB gpio pin
		lw		t1, 0(t0)
		or		t1,t1,t2
		sw		t1,0(t0)
#if(GPIO_OUTPUT_5V == 0)
#Dis STB GPIO output
		lw		t1, 4(t0)
		li		t2, 1
		sllv		t2, t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis STB output mode for pull high STB pin		
#endif
		
#delay		
		jal		delay_us_1668
		li		t6,5

#Pull low STB gpio pin		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG
#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
		lw		t1, 0(t0)
		not		t2
		and		t1,t1,t2
		sw		t1,0(t0)

#delay		
		jal		delay_us_1668
		li		t6,5
		
#write inc addr		
		li		a0, CMD1_WRITE_INCRE
		move		a1, a2
		jal		gpio_send_byte_sm1668	
#delay		
		jal		delay_us_1668
		li		t6,5
		
#Get STB gpio num										
		move		t2, a2
		andi		a1, a1, 0x00
#		ori		a1, a1, 0x45  // 
		get_stb_gpio a1
		

# Read GPIO DO reg									 
		andi		t2, a1, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a1, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:				
		andi		a1, 0x1f		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG

#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
#Pull high STB gpio pin
		lw		t1, 0(t0)
		or		t1,t1,t2
		sw		t1,0(t0)
#if(GPIO_OUTPUT_5V == 0)
#Dis STB GPIO output
		lw		t1, 4(t0)
		li		t2, 1
		sllv		t2, t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis STB output mode for pull high STB pin		
#endif		
#delay		
		jal		delay_us_1668
		li		t6,5

#Pull low STB gpio pin		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG
#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
		lw		t1, 0(t0)
		not		t2
		and		t1,t1,t2
		sw		t1,0(t0)

#delay		
		jal		delay_us_1668
		li		t6,5
		nop		
		nop 
		nop 
#		sdbbp
		nop 
		nop 	
#write start addr		
		li	a0, (CMD2_PREFIX|6)
		move	a1, a2
		jal	gpio_send_byte_sm1668
		nop	
		nop		
		nop 
		nop 
#		sdbbp
		nop 
		nop 			
#write 0		
		move	t0, t4		# led_array
		move	t3, s6
		andi	t3, 0xf
		addu	t0, t0, t3
		lbu	t3, (t0)
		sll		a0,t3,START_SG_TO_LED
		move	a1, a2
		jal	gpio_send_led_data_sm1668
		nop
		nop 
		nop 
#		sdbbp
		nop 
		nop 	

#########	first index-2##################
#delay		
		jal		delay_us_1668
		li		t6,5
		
#Get STB gpio num										
		move		t2, a2
		andi		a1, a1, 0x00
#		ori		a1, a1, 0x45  // 
		get_stb_gpio a1
	

# Read GPIO DO reg									 
		andi		t2, a1, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a1, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a1, 0x1f		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG

#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
#Pull high STB gpio pin
		lw		t1, 0(t0)
		or		t1,t1,t2
		sw		t1,0(t0)
#if(GPIO_OUTPUT_5V == 0)
#Dis STB GPIO output
		lw		t1, 4(t0)
		li		t2, 1
		sllv		t2, t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis STB output mode for pull high STB pin		
#endif
		
#delay		
		jal		delay_us_1668
		li		t6,5

#Pull low STB gpio pin		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG
#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
		lw		t1, 0(t0)
		not		t2
		and		t1,t1,t2
		sw		t1,0(t0)

#delay		
		jal		delay_us_1668
		li		t6,5
		
#write inc addr		
		li		a0, CMD1_WRITE_INCRE
		move		a1, a2
		jal		gpio_send_byte_sm1668	
#delay		
		jal		delay_us_1668
		li		t6,5
		
#Get STB gpio num										
		move		t2, a2
		andi		a1, a1, 0x00
#		ori		a1, a1, 0x45  // 
		get_stb_gpio a1
		

# Read GPIO DO reg									 
		andi		t2, a1, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a1, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:				
		andi		a1, 0x1f		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG

#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
#Pull high STB gpio pin
		lw		t1, 0(t0)
		or		t1,t1,t2
		sw		t1,0(t0)
#if(GPIO_OUTPUT_5V == 0)
#Dis STB GPIO output
		lw		t1, 4(t0)
		li		t2, 1
		sllv		t2, t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis STB output mode for pull high STB pin		
#endif		
#delay		
		jal		delay_us_1668
		li		t6,5

#Pull low STB gpio pin		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG
#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
		lw		t1, 0(t0)
		not		t2
		and		t1,t1,t2
		sw		t1,0(t0)

#delay		
		jal		delay_us_1668
		li		t6,5

#write start addr		
		li	a0, (CMD2_PREFIX|4)
		move	a1, a2
		jal	gpio_send_byte_sm1668
		nop				
#write 1	
		move	t0, t4		# led_array
		move	t3, s6
		andi	t3, 0xf0
		srl t3,4
		addu	t0, t0, t3
		lbu	t3, (t0)
		#move	a0, t3
		sll		a0,t3,START_SG_TO_LED
		move	a1, a2
		jal	gpio_send_led_data_sm1668
		nop	
		nop
		nop 
		nop 
#		sdbbp
		nop 
		nop 	
#########	first index-3##################		
#delay		
		jal		delay_us_1668
		li		t6,5
		
#Get STB gpio num										
		move		t2, a2
		andi		a1, a1, 0x00
	#	ori		a1, a1, 0x45  // 
		get_stb_gpio a1
			

# Read GPIO DO reg									 
		andi		t2, a1, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a1, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a1, 0x1f		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG

#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
#Pull high STB gpio pin
		lw		t1, 0(t0)
		or		t1,t1,t2
		sw		t1,0(t0)
#if(GPIO_OUTPUT_5V == 0)
#Dis STB GPIO output
		lw		t1, 4(t0)
		li		t2, 1
		sllv		t2, t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis STB output mode for pull high STB pin		
#endif
		
#delay		
		jal		delay_us_1668
		li		t6,5

#Pull low STB gpio pin		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG
#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
		lw		t1, 0(t0)
		not		t2
		and		t1,t1,t2
		sw		t1,0(t0)

#delay		
		jal		delay_us_1668
		li		t6,5
		
#write inc addr		
		li		a0, CMD1_WRITE_INCRE
		move		a1, a2
		jal		gpio_send_byte_sm1668	
#delay		
		jal		delay_us_1668
		li		t6,5
		
#Get STB gpio num										
		move		t2, a2
		andi		a1, a1, 0x00
#		ori		a1, a1, 0x45  
		get_stb_gpio a1


# Read GPIO DO reg									 
		andi		t2, a1, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a1, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:				
		andi		a1, 0x1f		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG

#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
#Pull high STB gpio pin
		lw		t1, 0(t0)
		or		t1,t1,t2
		sw		t1,0(t0)
#if(GPIO_OUTPUT_5V == 0)
#Dis STB GPIO output
		lw		t1, 4(t0)
		li		t2, 1
		sllv		t2, t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis STB output mode for pull high STB pin		
#endif		
#delay		
		jal		delay_us_1668
		li		t6,5

#Pull low STB gpio pin		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG
#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
		lw		t1, 0(t0)
		not		t2
		and		t1,t1,t2
		sw		t1,0(t0)

#delay		
		jal		delay_us_1668
		li		t6,5

#write start addr		
		li	a0, (CMD2_PREFIX|2)
		move	a1, a2
		jal	gpio_send_byte_sm1668
		nop			
#write 2	
		move	t0, t4		# led_array
		move	t3, s6
		andi	t3, 0xf00
		srl	t3, 8
		addu	t0, t0, t3
		lbu	t3, (t0)
#		sll	t3, 2

		move 	t2,s6
		srl  	  	t2,16
		and    	t2,0x00000006
		beqz   	t2,1f
		nop
		or       	t3,0x80

1:
		sll		a0,t3,START_SG_TO_LED
		li		t0,1
		beqz	t9,2f
		nop
		nop
		sll		t0,RED_LED_CONTROL_SG       #show red led
		b		3f
		nop
		nop
2:
		sll		t0,GRE_LED_CONTROL_SG       #show Green led
3:
		or		a0,a0,t0
		move	a1, a2
		jal	gpio_send_led_data_sm1668
		nop	
		nop 
		nop 
#		sdbbp
		nop 
		nop 			
#########	first index-4##################	
#delay		
		jal		delay_us_1668
		li		t6,5
		
#Get STB gpio num										
		move		t2, a2
		andi		a1, a1, 0x00
#		ori		a1, a1, 0x45  
		get_stb_gpio a1
		

# Read GPIO DO reg									 
		andi		t2, a1, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a1, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a1, 0x1f		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG

#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
#Pull high STB gpio pin
		lw		t1, 0(t0)
		or		t1,t1,t2
		sw		t1,0(t0)
#if(GPIO_OUTPUT_5V == 0)
#Dis STB GPIO output
		lw		t1, 4(t0)
		li		t2, 1
		sllv		t2, t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis STB output mode for pull high STB pin		
#endif
		
#delay		
		jal		delay_us_1668
		li		t6,5

#Pull low STB gpio pin		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG
#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
		lw		t1, 0(t0)
		not		t2
		and		t1,t1,t2
		sw		t1,0(t0)

#delay		
		jal		delay_us_1668
		li		t6,5
		
#write inc addr		
		li		a0, CMD1_WRITE_INCRE
		move		a1, a2
		jal		gpio_send_byte_sm1668	
#delay		
		jal		delay_us_1668
		li		t6,5
		
#Get STB gpio num										
		move		t2, a2
		andi		a1, a1, 0x00
#		ori		a1, a1, 0x45  
		get_stb_gpio a1
		

# Read GPIO DO reg									 
		andi		t2, a1, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a1, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:				
		andi		a1, 0x1f		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG

#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
#Pull high STB gpio pin
		lw		t1, 0(t0)
		or		t1,t1,t2
		sw		t1,0(t0)
#if(GPIO_OUTPUT_5V == 0)
#Dis STB GPIO output
		lw		t1, 4(t0)
		li		t2, 1
		sllv		t2, t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis STB output mode for pull high STB pin		
#endif		
#delay		
		jal		delay_us_1668
		li		t6,5

#Pull low STB gpio pin		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG
#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
		lw		t1, 0(t0)
		not		t2
		and		t1,t1,t2
		sw		t1,0(t0)

#delay		
		jal		delay_us_1668
		li		t6,5

#write start addr		
		li	a0, (CMD2_PREFIX|0)
		move	a1, a2
		jal	gpio_send_byte_sm1668
#write 3	
		nop						
		nop		
		nop 
#		sdbbp
		nop
		nop 
		move	t0, t4		# led_array
		move	t3, s6
		andi	t3, 0xf000
		srl	t3, 12
		addu	t0, t0, t3

		lbu	t3, (t0)
		sll		a0,t3,START_SG_TO_LED
		move	a1, a2
		jal	gpio_send_led_data_sm1668
		nop						
		nop		
		nop 
#		sdbbp
		nop
		nop 
#delay						
		jal		delay_us_1668
		li		t6,5			
		
#Get STB gpio num								
		move		t2, a2
		andi		a1, a1, 0x00
#		ori		a1, a1, 0x45  
		get_stb_gpio a1
		

# Read GPIO DO reg									 
		andi		t2, a1, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a1, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a1, 0x1f		
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG

#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
#Pull high STB gpio pin
		lw		t1, 0(t0)
		or		t1,t1,t2
		sw		t1,0(t0)	
#if(GPIO_OUTPUT_5V == 0)
#Dis STB GPIO output
		lw		t1, 4(t0)
		li		t2, 1
		sllv		t2, t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis STB output mode for pull high STB pin		
#endif
		
		move	ra, t7
show_led_exit:		
		jr	ra
		nop		
		.end show_led_sm1668
		
####################check_key_sm1668 #####################
#		used a0,a1,a3,t0,t1,t2,t6,t7,ra
#		UINT32 check_key_sm1668(struct pan_hw_info *pan_hw_info)
#		v0: return value, ==0 get power key;	a0: pan_hw_info
#####################################################						
		.globl	check_key_sm1668
		.ent	check_key_sm1668
check_key_sm1668:
# .word	0x7000003f
		.set	noreorder
		move		t7, ra
		move		a1, a0
		la      	t1, 3f					# Running in cache
 		sll		t1, 4
   		srl		t1, 4
   		lui		t0, 0x8000
   		or		t1, t0
   		jr      	t1
   		nop
3:
#Get STB gpio num										
		move		t2, a1
		andi		a3, a3, 0x00
#		ori		a3, a3, 0x45  
		get_stb_gpio a3
		

# Read GPIO DO reg									 
		andi		t2, a3, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a3, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a3, 0x1f		
		li		t2, 1
		sllv		t2,t2,a3	#a3 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG

#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
#Pull high STB gpio pin
		lw		t1, 0(t0)
		or		t1,t1,t2
		sw		t1,0(t0)
#if(GPIO_OUTPUT_5V == 0)
#Dis STB GPIO output
		lw		t1, 4(t0)
		li		t2, 1
		sllv		t2, t2,a3	#a3 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis STB output mode for pull high STB pin		
#endif
		
#Pull low STB gpio pin	
		li		t2, 1
		sllv		t2,t2,a3	#a3 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG
#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
			
		lw		t1, 0(t0)
		not		t2
		and		t1,t1,t2
		sw		t1,0(t0)
				
#delay		
		jal		delay_us_1668
		li		t6,5
		
		li		a0, CMD1_READ
		jal		gpio_send_byte_sm1668
		nop
		
#delay				
		jal		delay_us_1668
		li		t6,2
#get byte0
		move		a0, a1
		jal		gpio_get_byte_sm1668
		nop		
		andi		v0, 0xff	
		beqz		v0, 1f
		nop
		b		3f	
		li		t6, 0
1:	
#get byte1
		jal		gpio_get_byte_sm1668
		nop	
		andi		v0, 0xff
		beqz		v0, 2f
		nop
		b		3f	
		li		t6, 8
2:
#get byte2	
		jal		gpio_get_byte_sm1668
		nop
		andi		v0, 0xff	
		beqz		v0, get_byte_3_extern
		nop
		b		3f			
		li		t6, 16
get_byte_3_extern	:
		jal		gpio_get_byte_sm1668
		nop
		andi		v0, 0xff	
		beqz		v0, get_byte_4_extern
		nop
		b		3f			
		li		t6, 24	
get_byte_4_extern	:
		jal		gpio_get_byte_sm1668
		nop
		andi		v0, 0xff	
		beqz		v0, 6f
		nop
		li		t6, 32			
3:
		li		t0, 0
		li		t2, 8
4:		
		srl		t1, v0, t0
		andi		t1, 1
		bnez		t1, 5f
		nop
		addi		t0, 1
		blt		t0, t2, 4b
		nop
		b		6f
		nop
5:
		add		t6, t0
		lui		v0, 0x0
		li		t1, 1
		sll		t1, t1, t6
		or		v0, v0, t1
		b		7f
		nop
		
6:
		li		v0, PAN_KEY_INVALID
7:				
#delay						
		jal		delay_us_1668
		li		t6,5			
		
#Get STB gpio num
#.word 0x7000003f								
		move		t2, a1
		andi		a3, a3, 0x00
#		ori		a3, a3, 0x45   
		get_stb_gpio a3
	

# Read GPIO DO reg									 
		andi		t2, a3, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a3, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a3, 0x1f		
		li		t2, 1
		sllv		t2,t2,a3	#a3 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG

#En STB GPIO output
		lw		t1, 4(t0)													
		or		t1,t2
		sw		t1, 4(t0)
		
#Pull high STB gpio pin
		lw		t1, 0(t0)
		or		t1,t1,t2
		sw		t1,0(t0)
#if(GPIO_OUTPUT_5V == 0)
#Dis STB GPIO output
		lw		t1, 4(t0)
		li		t2, 1
		sllv		t2, t2,a3	#a3 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_EN_REG
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis STB output mode for pull high STB pin		
#endif

#return
		move	ra,t7
		jr		ra
		nop

		.set	reorder
		.end	check_key_sm1668

#		used t6,t7	

####################pan_init_sm1668 #####################
#		used a0,a1,t0,t1,t2
#		void pan_init_sm1668(struct pan_hw_info *pan_hw_info) 
#####################################################		
		.globl	pan_init_sm1668
		.ent		pan_init_sm1668
pan_init_sm1668:
#Get STB gpio num										
		move		t2, a0
		andi		a1, a1, 0x00
#		ori		a1, a1, 0x45  
		get_stb_gpio a1
		

# Read GPIO DO reg									 
		andi		t2, a1, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a1, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a1, 0x1f	# Only 32 GPIO
		li		t2, 1
		sllv		t2,t2,a1	#a1 = STB gpio number, t2 = STB gpio mask, t0 = STB GPIO_DO_REG

#Pull high STB gpio pin
		lw		t1, 0(t0)
		or		t1,t1,t2
		sw		t1,0(t0)

#if(GPIO_OUTPUT_5V == 1)
#En STB gpio output
		lw		t1, 4(t0)
		or		t1,t1,t2
		sw		t1,4(t0)	
#else	# (GPIO_OUTPUT_5V == 0)
#Dis STB GPIO output
		lw		t1, 4(t0)
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis STB output mode for pull high STB pin		
#endif			
#Get CLK gpio num										
		move		t2, a0
		andi		a1, a1, 0x00
	#	ori		a1, a1, 0x3e  
		get_clk_gpio a1
		

# Read GPIO DO reg									 
		andi		t2, a1, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a1, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a1, 0x1f
		li		t2, 1
		sllv		t2,t2,a1	#a1 = CLK gpio number, t2 = CLK gpio mask, t0 = CLK GPIO_DO_REG

#Pull high CLK gpio pin
		lw		t1, 0(t0)
		or		t1,t1,t2
		sw		t1,0(t0)
#if(GPIO_OUTPUT_5V == 1)
#En CLK gpio output
		lw		t1, 4(t0)
		or		t1,t1,t2
		sw		t1,4(t0)
#else
#Dis CLK GPIO output
		lw		t1, 4(t0)
		not		t2
		and		t1, t2
		sw		t1, 4(t0) #Dis CLK output mode for pull high CLK pin		
#endif			
		
#Get DIN gpio num										
		move		t2, a0
		andi		a1, a1, 0x00
#		ori		a1, a1, 0x4b  
		get_data_gpio a1


# Read GPIO DO reg	
		andi		t2, a1, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a1, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a1, 0x1f
		li		t2, 1
		sllv		t2,t2,a1	#a1 = DIN gpio number, t2 = DIN gpio mask, t0 = DIN GPIO_DO_REG
		
#En DIN gpio output
		lw		t1, 4(t0)
		or		t1,t1,t2
		sw		t1,4(t0)

#Get DOUT gpio num										
		move		t2, a0
		andi		a1, a1, 0x00
#		ori		a1, a1, 0x4b  
		get_data_gpio a1
	

# Read GPIO DO reg									 
		andi		t2, a1, 0x40
		beq		t2, zero, 1f
		nop
		li		t0, HAL_GPIO2_DO_REG		
		j		2f
		nop
1:		
		li		t0, HAL_GPIO_DO_REG
		andi		t2, a1, 0x20
		beq		t2, zero, 2f
		nop
		li		t0, HAL_GPIO1_DO_REG
2:		
		andi		a1, 0x1f
		li		t2, 1
		sllv		t2,t2,a1	#a1 = DOUT gpio number, t2 = DOUT gpio mask, t0 = DOUT GPIO_DO_REG
		
#Dis DOUT gpio output
		lw		t1, 4(t0)
		not		t2
		and		t1,t1,t2
		sw		t1,4(t0)
#return		
		jr	ra
		nop
		.end		pan_init_sm1668
####################Delay US #####################
#		
################################################	
		.globl	delay_us_1668
		.ent	delay_us_1668
delay_us_1668:
		.set	noreorder
/*1:		li	t0, 0x200
2:		bne	t0,zero,2b
		addiu	t0, -1
		addiu	t6, -1
		bnez	t6, 1b
		nop
	*/	
	
/*	
		mtc0	k0, C0_ERROREPC
		li	k0, 0x80000710
		sw	k1, 0x0(k0)

		sw	t0, 0x4(k0)	
		sw	t1, 0x8(k0) 
		sw	t2, 0xC(k0) 
		sw	t3, 0x10(k0)
		sw	t4, 0x14(k0)
		sw	t5, 0x18(k0)
		sw	t6, 0x1C(k0)
		sw	t7, 0x20(k0)
		sw	t8, 0x24(k0)
		sw	t9, 0x28(k0)
		sw	v0, 0x2C(k0)
		sw	v1, 0x30(k0)
		sw	a0, 0x34(k0)
		sw	a1, 0x38(k0)
		sw	a2, 0x3C(k0)
		sw	a3, 0x40(k0)
		sw	AT, 0x44(k0)
		sw	ra, 0x48(k0)
		sw	s0, 0x4C(k0)
		sw	s1, 0x50(k0)
		
		mfc0    k1, C0_SR				# Enable interrupt for IR
		nop
		ori     k1, 1
		mtc0    k1, C0_SR
		nop

		nop
		nop
		nop
		nop
		
		mfc0    k1, C0_SR				# Disable interrupt when scan LED
		srl	k1, 1
		sll	k1, 1
		mtc0	k1, C0_SR
		nop
		
		li	k0, 0x80000710
		lw	t0, 0x4(k0)
		lw	t1, 0x8(k0)
		lw	t2, 0xc(k0)
		lw	t3, 0x10(k0)
		lw	t4, 0x14(k0)
		lw	t5, 0x18(k0)
		lw	t6, 0x1c(k0)
		lw	t7, 0x20(k0)
		lw	t8, 0x24(k0)
		lw	t9, 0x28(k0)
		lw	v0, 0x2c(k0)
		lw	v1, 0x30(k0)
		lw	a0, 0x34(k0)
		lw	a1, 0x38(k0)
		lw	a2, 0x3c(k0)
		lw	a3, 0x40(k0)
		lw	AT, 0x44(k0)
		lw	ra, 0x48(k0)
		lw	s0, 0x4c(k0)
		lw	s1, 0x50(k0)
		
		lw	k1, 0(k0)
		mfc0 	k0, C0_ERROREPC
*/		
		jr	ra
		nop
		.set	reorder
		.end	delay_us_1668
	
